import BaseView from "../../../cfw/mvc/BaseView";
// import BadRoomStepView from "./BadRoomStepView";
import GlobalEvent from "../../../cfw/event/GlobalEvent";
import { EventName, ActionType } from "../../../public/Config";
import ModuleManager from "../../../cfw/module/ModuleManager";
import BadRoomController from "../BadRoomController";
import AddPlayer from "../../player/AddPlayer";
import BadRoomManager from "../model/BadRoomManager";
import SDKManager from "../../../sdk/SDKManager";
import { SDKEventName } from "../../../sdk/SDKConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BadRoomView extends BaseView {
    @property(cc.Node)
    content: cc.Node = null;


    // @property(AddPlayer)
    // player: AddPlayer = null;
    // @property([BadRoomStepView])
    // steps: BadRoomStepView[] = []

    @property(cc.Sprite)
    dreamBg: cc.Sprite = null;
    private step: number = 0;
    private len: number = 3;
    protected childComp: cc.Component;
    protected controller: BadRoomController;
    protected model: BadRoomManager;
    start() {
        this.dreamBg.node.active = false;
        GlobalEvent.instance().on(EventName.UPDATE_SLEEP_STEP, this.updateStep, this)
        this.startStep(this.step)
    }

    onButtonBackClick() {
        this.controller.back()
    }

    onDestroy() {
        GlobalEvent.instance().off(EventName.UPDATE_SLEEP_STEP, this.updateStep, this)
    }

    startStep(n: number) {
        cc.log(' BadRoomView startStep n ', n)
        // this.steps[n].start()

        // if (this.childComp) {
        //     this.childComp.node.destroy()
        //     this.childComp = null;
        // }
        if (n == this.len - 1) {
            this.dreamBg.node.active = true;
            // cc.log(' BadRoomView startStep n 1111 ', n)
            // this.player.setAction(ActionType.SLEEP)
            GlobalEvent.instance().emit(EventName.CHANGE_PLAYER_ACTION, ActionType.SLEEP)
            // cc.log('BadRoomView  startStep n 2222', n)            
        }
        this.controller.addToParent('prefabs/BadRoomStepView' + n, 'BadRoomStepView' + n, this.model, ModuleManager.getLoader(), this.content, (comp) => {
            this.childComp = comp
        })
    }

    updateStep(step: number) {
        if (!cc.isValid(this.node)) {
            return;
        }
        this.step = step;
        // this.steps[this.step].node.active = false;
        // this.steps[step].node.active = false;
        SDKManager.getChannel().sendEvent(SDKEventName.badroom, this.step)
        this.step++;
        if (this.step >= this.len) {
            this.model.updateExp()
            this.controller.back()
        } else {
            this.startStep(this.step)
        }


    }


    // update (dt) {}
}
